<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系统用户" prop="sysUserName">
        <el-input
          v-model="queryParams.sysUserName"
          placeholder="请输入系统用户"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三方账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入三方账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入用户邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户来源" prop="source">
        <el-select v-model="queryParams.source" placeholder="请选择用户来源" clearable style="width: 200px">
          <el-option
            v-for="dict in sys_social_auth"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['auth:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['auth:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="三方用户ID" align="center" prop="uuid" v-if="columns[0].visible" />
      <el-table-column label="系统用户" align="center" prop="sysUserName" v-if="columns[1].visible" />
      <el-table-column label="三方账号" align="center" prop="userName" v-if="columns[2].visible" />
      <el-table-column label="用户昵称" align="center" prop="nickName" v-if="columns[3].visible" />
      <el-table-column label="头像" align="center" prop="avatar" v-if="columns[4].visible">
        <template #default="scope">
          <img v-if="scope.row.avatar" :src="scope.row.avatar" style="width: 45px; height: 45px" alt="头像" />
          <img v-else :src="avatar" style="width: 45px; height: 45px" alt="头像" />
        </template>
      </el-table-column>
      <el-table-column label="用户邮箱" align="center" prop="email" v-if="columns[5].visible" />
      <el-table-column label="用户来源" align="center" prop="source" v-if="columns[6].visible">
        <template #default="scope">
          <dict-tag :options="sys_social_auth" :value="scope.row.source"/>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="User">
import { listUser, delUser } from '@/api/social/user';
import avatar from '@/assets/images/avatar.png';

const { proxy } = getCurrentInstance();
const { sys_social_auth } = proxy.useDict('sys_social_auth');

const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 列显隐信息
const columns = ref([
  { key: 0, label: `三方用户ID`, visible: true },
  { key: 1, label: `系统用户`, visible: true },
  { key: 2, label: `三方账号`, visible: true },
  { key: 3, label: `用户昵称`, visible: true },
  { key: 4, label: `头像地址`, visible: true },
  { key: 5, label: `用户邮箱`, visible: true },
  { key: 6, label: `用户来源`, visible: true },
]);

const data = reactive({
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sysUserName: null,
    userName: null,
    nickName: null,
    email: null,
    source: null,
  }
});

const { queryParams } = toRefs(data);

/** 查询第三方认证用户列表 */
function getList() {
  loading.value = true;
  listUser(queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.authId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _authIds = row.authId || ids.value;
  proxy.$modal.confirm('是否确认删除第三方认证用户编号为"' + _authIds + '"的数据项？').then(function() {
    return delUser(_authIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('auth/user/export', {
    ...queryParams.value
  }, `user_${new Date().getTime()}.xlsx`)
}

getList();
</script>
